package com.site.dao.impl;

import com.site.dao.ReceiveDetailDao;
import com.site.pojo.ReceiveDetail;
import com.site.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 入库明细表Dao实现类
 */
public class ReceiveDetailDaoImpl implements ReceiveDetailDao {
    //查询所有
    @Override
    public List<ReceiveDetail> selectAll() {
        String sql = "select * from receive_detail";
        List<ReceiveDetail> list = new ArrayList<ReceiveDetail>();
        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            rs = JDBCUtils.executeQuery(conn,sql);
            while (rs.next()){
                ReceiveDetail receiveDetail = new ReceiveDetail();
                receiveDetail.setId(rs.getLong("id"));
                receiveDetail.setRid(rs.getLong("rid"));
                receiveDetail.setEid(rs.getLong("eid"));
                receiveDetail.setGoodsId(rs.getLong("goods_id"));
                receiveDetail.setRtid(rs.getLong("rtid"));
                receiveDetail.setReceiveCount(rs.getLong("receive_count"));
                receiveDetail.setUnit(rs.getString("unit"));
                receiveDetail.setOprTime(rs.getTimestamp("opr_time"));
                list.add(receiveDetail);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,null,conn);
        }
        return list;
    }
    //按id查询
    @Override
    public ReceiveDetail selectByid(Long id) {
        String sql = "select * from receive_detail where id=?";
        ReceiveDetail receiveDetail = new ReceiveDetail();
        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            rs = JDBCUtils.executeQuery(conn,sql,id);
            while (rs.next()){
                receiveDetail.setId(rs.getLong("id"));
                receiveDetail.setRid(rs.getLong("rid"));
                receiveDetail.setEid(rs.getLong("eid"));
                receiveDetail.setGoodsId(rs.getLong("goods_id"));
                receiveDetail.setRtid(rs.getLong("rtid"));
                receiveDetail.setReceiveCount(rs.getLong("receive_count"));
                receiveDetail.setUnit(rs.getString("unit"));
                receiveDetail.setOprTime(rs.getTimestamp("opr_time"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,null,conn);
        }
        return receiveDetail;
    }
    //查询一个入库单下所属的入库明细单
    @Override
    public List<ReceiveDetail> selectByrid(Long rid) {
        String sql = "select * from receive_detail where rid=?";
        List<ReceiveDetail> list = new ArrayList<ReceiveDetail>();
        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            rs = JDBCUtils.executeQuery(conn,sql,rid);
            while (rs.next()){
                ReceiveDetail receiveDetail = new ReceiveDetail();
                receiveDetail.setId(rs.getLong("id"));
                receiveDetail.setRid(rs.getLong("rid"));
                receiveDetail.setEid(rs.getLong("eid"));
                receiveDetail.setGoodsId(rs.getLong("goods_id"));
                receiveDetail.setRtid(rs.getLong("rtid"));
                receiveDetail.setReceiveCount(rs.getLong("receive_count"));
                receiveDetail.setUnit(rs.getString("unit"));
                receiveDetail.setOprTime(rs.getTimestamp("opr_time"));
                list.add(receiveDetail);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,null,conn);
        }
        return list;
    }
    //无事务,新增一个入库明细单
    @Override
    public int addReceiveDetail(ReceiveDetail receiveDetail) {
        int num = 0;
        String sql = "insert into receive_detail(rid,eid,goods_id,rtid,receive_count,unit,opr_time)" +
                " values(?,?,?,?,?,?,?)";
        Object[] param = {receiveDetail.getRid(),receiveDetail.getEid(),receiveDetail.getGoodsId()
        ,receiveDetail.getRtid(),receiveDetail.getReceiveCount(),receiveDetail.getUnit(),receiveDetail.getOprTime()};
        try{
            num = JDBCUtils.executeUpdate(sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //有事务,新增一个入库明细单
    @Override
    public int addReceiveDetail(Connection conn, ReceiveDetail receiveDetail) {
        int num = 0;
        String sql = "insert into receive_detail(rid,eid,goods_id,rtid,receive_count,unit,opr_time) " +
                "values(?,?,?,?,?,?,?)";
        Object[] param = {receiveDetail.getRid(),receiveDetail.getEid(),receiveDetail.getGoodsId()
                ,receiveDetail.getRtid(),receiveDetail.getReceiveCount(),receiveDetail.getUnit(),receiveDetail.getOprTime()};
        try{
            num = JDBCUtils.executeUpdate(conn,sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    //无事务,通过id删除入库明细单
    @Override
    public int deleteReceiveDetailByid(Long id) {
        int num = 0;
        String sql = "delete from receive_detail where id=?";
        Object[] param = {id};
        try{
            num = JDBCUtils.executeUpdate(sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //有事务,通过id删除入库明细单
    @Override
    public int deleteReceiveDetailByid(Connection conn, Long id) {
        int num = 0;
        String sql = "delete from receive_detail where id=?";
        Object[] param = {id};
        try{
            num = JDBCUtils.executeUpdate(conn,sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    //无事务,通过receiveDetail.id修改入库明细单
    @Override
    public int updateReceiveDetail(ReceiveDetail receiveDetail) {
        int num = 0;
        String sql = "update receive_detail set rid=?,eid=?,goods_id=?,rtid=?,receive_count=?,unit=?,opr_time=?" +
                " where id=?";
        Object[] param = {receiveDetail.getRid(),receiveDetail.getEid(),receiveDetail.getGoodsId()
        ,receiveDetail.getRtid(),receiveDetail.getReceiveCount(),receiveDetail.getUnit(),receiveDetail.getOprTime(),
        receiveDetail.getId()};
        try{
            num = JDBCUtils.executeUpdate(sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //有事务,通过receiveDetail.id修改入库明细单
    @Override
    public int updateReceiveDetail(Connection conn, ReceiveDetail receiveDetail) {
        int num = 0;
        String sql = "update receive_detail set rid=?,eid=?,goods_id=?,rtid=?,receive_count=?,unit=?,opr_time=?" +
                " where id=?";
        Object[] param = {receiveDetail.getRid(),receiveDetail.getEid(),receiveDetail.getGoodsId()
                ,receiveDetail.getRtid(),receiveDetail.getReceiveCount(),receiveDetail.getUnit(),receiveDetail.getOprTime(),
                receiveDetail.getId()};
        try{
            num = JDBCUtils.executeUpdate(conn,sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
}
